package org.lanyu.springainovel.novel.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lanyu.springainovel.common.vo.RestVO;
import org.lanyu.springainovel.novel.entity.BookOutline;
import org.lanyu.springainovel.novel.service.NovelOutlineService;
import org.lanyu.springainovel.novel.vo.NovelOutlineRequest;
import org.lanyu.springainovel.novel.vo.OutlineUpdateRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * 小说大纲控制器
 */
@Slf4j
@RestController
@RequestMapping("/novel/outline")
@AllArgsConstructor
public class NovelOutlineController {

    private final NovelOutlineService novelOutlineService;

    /**
     * 流式生成小说大纲
     *
     * @param request 大纲生成请求
     * @return 流式大纲内容
     */
    @PostMapping(value = "/generate/stream", produces = MediaType.TEXT_PLAIN_VALUE + ";charset=UTF-8")
    public Flux<String> generateOutlineStream(@RequestBody NovelOutlineRequest request) {
        try {
            // 调用服务生成流式大纲
            return novelOutlineService.generateNovelOutlineStream(request);
        } catch (Exception e) {
            log.error("流式生成大纲失败", e);
            return Flux.just("生成大纲失败: " + e.getMessage());
        }
    }

    @Operation(summary = "根据书籍ID查询大纲列表", description = "根据书籍ID查询小说大纲的详细信息，包括主线情节、支线情节和人物设定，按版本号倒序返回")
    @GetMapping("/list/{bookId}")
    public RestVO<List<BookOutline>> getOutlineByBookId(@PathVariable Long bookId) {
        try {
            // 查询大纲信息（按版本号倒序）
            List<BookOutline> outlines = novelOutlineService.getOutlinesByBookIdOrderByVersionDesc(bookId);
            if (outlines == null || outlines.isEmpty()) {
                return RestVO.fail("该书籍暂无大纲信息");
            }

            return RestVO.success(outlines);
        } catch (Exception e) {
            log.error("查询大纲详情失败", e);
            return RestVO.fail("查询大纲详情失败: " + e.getMessage());
        }
    }

    @Operation(summary = "查询书籍指定版本的大纲详情", description = "查询书籍的指定版本大纲")
    @GetMapping("/{bookId}/{version}")
    public RestVO<BookOutline> getChapterOutlines(@PathVariable Long bookId, @PathVariable Long version) {
        try {
            // 查询指定版本的大纲信息
            BookOutline outline = novelOutlineService.getOutlineByBookVersion(bookId, version);
            if (outline == null) {
                return RestVO.fail("该书籍暂无大纲信息");
            }
            return RestVO.success(outline);
        } catch (Exception e) {
            log.error("查询章节大纲失败", e);
            return RestVO.fail("查询章节大纲失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除指定版本的大纲", description = "删除指定书籍和版本的大纲")
    @DeleteMapping("/{bookId}/{version}")
    public RestVO<Boolean> deleteOutlineByVersion(@PathVariable Long bookId, @PathVariable Long version) {
        try {
            // 调用服务层方法删除指定版本的大纲
            boolean result = novelOutlineService.deleteOutlineByVersion(bookId, version);

            return RestVO.success(result);
        } catch (Exception e) {
            log.error("删除大纲失败", e);
            return RestVO.fail("删除大纲失败: " + e.getMessage());
        }
    }

    @Operation(summary = "更新或新增大纲", description = "更新指定版本的大纲内容，如果版本不存在则新增")
    @PostMapping("/saveOrUpdate")
    public RestVO<BookOutline> updateOrAddOutline(@RequestBody OutlineUpdateRequest request) {
        try {
            BookOutline bookOutline = novelOutlineService.saveOrUpdateOutline(request);
            return RestVO.success(bookOutline);
        } catch (Exception e) {
            log.error("更新或新增大纲失败", e);
            return RestVO.fail("更新或新增大纲失败: " + e.getMessage());
        }
    }

    @Operation(summary = "归档大纲", description = "创建新版本的大纲")
    @PostMapping("/archive")
    public RestVO<BookOutline> archiveOutline(@RequestBody OutlineUpdateRequest request) {
        try {

            // 调用服务层方法归档大纲
            BookOutline result = novelOutlineService.archiveOutline(
                    request.getBookId(),
                    request.getOutlineDetails(),
                    request.getChapterPlanning(),
                    request.getVersion()
            );

            return RestVO.success(result);
        } catch (Exception e) {
            log.error("归档大纲失败", e);
            return RestVO.fail("归档大纲失败: " + e.getMessage());
        }
    }


}